 <!--
- costlib.php
- v0.02 iteration 2
- 2/10/2011
- This is a php file containing database functions for cost manipulations.
-->

<?php
function updateCost($mainType,$sampleType,$analysisType,$costChange,$database,$host,$login,$loginPassword){
	$con = mysql_connect($host,$login,$loginPassword) or die('Could not connect: ' . mysql_error());
	mysql_select_db($database) or die(mysql_error());
	$returnValue = mysql_query("UPDATE cost_analysis SET cost = $costChange where main_type = '". $mainType."'  and sample_type = '". $sampleType
			. "' and analysis_type = '". $analysisType."'",$con )or die(mysql_error());
	mysql_close($con);
	return $returnValue;
}

function getCost($mainType,$sampleType,$analysisType,$database,$host,$login,$loginPassword){
	$con = mysql_connect($host,$login,$loginPassword) or die('Could not connect: ' . mysql_error());
	mysql_select_db($database) or die(mysql_error());
	$result = mysql_query("SELECT cost FROM cost_analysis WHERE main_type = '". $mainType."'  and sample_type = '". $sampleType
			. "' and analysis_type = '". $analysisType."'" ) or die(mysql_error());
	$returnValue = mysql_fetch_array($result) ;	
	mysql_close($con);
	return $returnValue[0];
}

function getTable($database,$host,$login,$loginPassword){
	$returnValue = array();
	$con = mysql_connect($host,$login,$loginPassword) or die('Could not connect: ' . mysql_error());
	mysql_select_db($database) or die(mysql_error());
	$result = mysql_query("SELECT * FROM cost_analysis" )or die(mysql_error());
	while ($row = mysql_fetch_row($result, MYSQL_NUM)){
		array_push($returnValue, $row[0]);
	}	
	mysql_close($con);
	return $returnValue;
}

function getLimitedTable($mainType,$sampleType,$database,$host,$login,$loginPassword){
	$returnValue = array();
	$con = mysql_connect($host,$login,$loginPassword) or die('Could not connect: ' . mysql_error());
	mysql_select_db($database) or die(mysql_error());
	$result = mysql_query("SELECT * FROM cost_analysis WHERE main_type = '$mainType' AND sample_type = '$sampleType'")or die(mysql_error());
	while ($row = mysql_fetch_row($result, MYSQL_NUM)){
		array_push($returnValue, $row);
	}
	mysql_close($con);
	return $returnValue;
}

function getMainTypes($database,$host,$login,$loginPassword){
	$returnValue = array();
	$con = mysql_connect($host,$login,$loginPassword) or die('Could not connect: ' . mysql_error());
	mysql_select_db($database) or die(mysql_error());
	$result = mysql_query("SELECT DISTINCT main_type FROM cost_analysis" )or die(mysql_error());
	while ($row = mysql_fetch_row($result, MYSQL_NUM)){
		array_push($returnValue, $row[0]);
	}
	mysql_close($con);
	return $returnValue;
}

function getSampleTypes($mainType,$database,$host,$login,$loginPassword){
	$returnValue = array();
	$con = mysql_connect($host,$login,$loginPassword) or die('Could not connect: ' . mysql_error());
	mysql_select_db($database) or die(mysql_error());
	$result = mysql_query("SELECT DISTINCT sample_type FROM cost_analysis WHERE main_type = '$mainType'" )or die(mysql_error());
	while ($row = mysql_fetch_row($result, MYSQL_NUM)){
		array_push($returnValue, $row[0]);
	}
	mysql_close($con);
	return $returnValue;
}

function getAnalysisTypes($mainType,$sampleType,$database,$host,$login,$loginPassword){
	$returnValue = array();
	$con = mysql_connect($host,$login,$loginPassword) or die('Could not connect: ' . mysql_error());
	mysql_select_db($database) or die(mysql_error());
	$result = mysql_query("SELECT DISTINCT analysis_type FROM cost_analysis WHERE main_type = '$mainType' AND sample_type = '$sampleType'" )or die(mysql_error());
	while ($row = mysql_fetch_row($result, MYSQL_NUM)){
		array_push($returnValue, $row[0]);
	}	
	mysql_close($con);
	return $returnValue;
}



/*Searches for users in the database.*/
	function LookFor($username, $password, $database, $host, $login,
			$loginpassword){
		//Create connection.
		$con = mysql_connect($host, $login, $loginpassword) or
			die('Could not connect: ' . mysql_error());
		mysql_select_db($database) or die(mysql_error());
		
		//Set-up and execute connection.
		$query = "SELECT * FROM login where username = '". $username."'";
		$result = mysql_query($query);
		$row = mysql_fetch_array($result) ;
		
		//Close the connection.
		mysql_close($con);
		if(strcmp($row['password'],$password) == 0) return $row['usertype'];
		else return FALSE;
		
	}
	  
	/*Inserts a new account in the database*/
	function InsertNewUser($username, $password, $usertype, $name, $title, 
			$position, $database, $host, $login, $loginpassword){ 
		//Check the database for redundant entries.
		$foundusers = LookFor($username, $password, $database, $host, $login,
			$loginpassword);
		
		//If an entry was found
		if(count($foundusers) > 0 && $foundusers["password"] != "")
			return FALSE;
		
		//Create connection.
		$con = mysql_connect($host, $login, $loginpassword) or
			die('Could not connect: ' . mysql_error());
		mysql_select_db($database) or die(mysql_error());
		
		//Insert the entry into the database.
		$loginQuery = "INSERT INTO login VALUES ('$username','$password','$usertype')";
		$userQuery = "INSERT INTO users VALUES ('$username','$name','$title','$position')";

		$login = mysql_query($loginQuery);
		$user = mysql_query($userQuery);
	
		//Close the connection.
		mysql_close($con);
		if($login == TRUE && $user == TRUE) return TRUE;
		else return FALSE;
	}
?>